package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 长江网解析
 * @author Persh
 *
 */
public class CjnNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.cjn.cn/[a-z]*/[a-z]*/[0-9]*.html",
		"http://www.cjdcw.com/[a-z]*/[0-9]*/t[0-9]*.htm",
//		"http://www.cjdcw.com/[a-z]*/"
		};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("h1"));
		tool.addParseLogic(new ParseLogic("td.artical"));
		tool.addParseLogic(new ParseLogic("div#title"));
		title=tool.parse();

		content= JSoupUtils.selectContent(url, doc, "div.con","td.p1","span#zoom","div#contents>div.dis_contact","span#Zoom","div.summary","div.cont","div.art-main","div.textCon","#contlist p",".nr3-2");


		tool.addParseLogic(new ParseLogic("div.wenjj"));
		tool.addParseLogic(new ParseLogic("td.b2"));
		tool.addParseLogic(new ParseLogic("span.time"));
		tool.addParseLogic(new ParseLogic("div#riqi"));
		tool.addParseLogic(new ParseLogic("div#contents>p.dis_time"));
		tool.addParseLogic(new ParseLogic("td.b5"));
		tool.addParseLogic(new ParseLogic("div.resoce>div.main02_ldt>span"));
		tool.addParseLogic(new ParseLogic("span.pub-time"));
		tool.addParseLogic(new ParseLogic(".time2"));
		tool.addParseLogic(new ParseLogic("#contlist h2"));
		tool.addParseLogic(new ParseLogic("dd"));
		date=tool.parseDate();

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		CjnNewsAnalyse analyse=new CjnNewsAnalyse();
		String url="http://www.cjn.cn/zx/redianxinwen/6242749231.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
